***********************************************************************
******
****** AKMP_Tables.do
******
****** Creates the tables and figures with results for the article: 
******
****** "Ambiguity Attitudes for Real-World Sources: Field Evidence from a Large Sample of Investors"
******
****** Authors: Kanin Anantanasuwong, Roy Kouwenberg, Olivia S. Mitchell, and Kim Peijnenburg
******
****** CURRENT VERSION:
****** Created  on 24 Jun 2019; by Roy Kouwenberg and Kanin Anantanasuwong
****** Modified on  6 Feb 2024; by Roy Kouwenberg (cleaned up for replication code)
****** 
****** Input files  : AKMP_Ambiguity_MainData.dta,  the main dataset for our article
****** 				  AKMP_Ambiguity_PanelData.dta, the dataset for our article in panel format
******
****** Output files : Separate files for the tables and figures with results in the article are saved in the working directory.
****** 		 Tables : AKMP_Table1.rtf, AKMP_Table2.rtf, AKMP_Table3.rtf, AKMP_Table4.rtf, AKMP_Table5.rtf, AKMP_TableA1.rtf, AKMP_TableB1.rtf 
****** 		Figures : AKMP_Figure3.png, AKMP_Figure4.png, AKMP_Figure5.png
******
****** Required Stata version and packages: 
****** This do code was written for Stata version 16. 
******     1. It uses "estat icc", which is only available in Stata version 16 or higher. 
******     2. Stata package "estout"  is used to create formatted tables in Word.
******     3. Stata package "grc1leg" is used to combine multple figures with one legend.
****** The two packages estout and grc1leg can be installed as follows in Stata:
******     ssc install estout, replace
******     net install grc1leg, from( http://www.stata.com/users/vwiggins/)
******

***********************************************************************
****** Set the working directory path and file names
***********************************************************************

** Set the working directory where the datafiles are located
** All tables and figures will also be saved in this dierectory
cd "D:\Mydocs\Papers\AKMP\Replication\Data"

** Set the filename for the processed main data file (regular format)
global fn_main "AKMP_Ambiguity_MainData.dta"

** Set the filename for the processed panel data (panel format)
global fn_panel "AKMP_Ambiguity_PanelData.dta"



***********************************************************************
****** Open the main dataset 
***********************************************************************

* Open the latest main dataset
use $fn_main, clear


***********************************************************************
****** Set the investors sample screens used for the results
***********************************************************************

* Set the sample screens for the main dataset
*
global screen0 beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
global screen0_noninvest beleg == 0 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
global screen0_all b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < .  & avg_riskprem < .

* Set the sample screens for the panel dataset
*
global screen0p beleg == 1 & b < . & a < . & avg_riskprem < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < .  
global screen2p beleg == 1 & b < . & a < . & a >= 0 & a <= 1 & avg_riskprem < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < .  


***********************************************************************
****** Set the main set of control variables for regressions and label them
***********************************************************************

* Main sets of control variables for the panel regressions
*
global controls_demo    oplcat age dfemale single_dummy occ_empl logchildren lognettohh logfinasshh_ms logfinasshh_md 
global controls_FLrisk  FL_Total b_risk a_risk 


*** Label all the variables for the tables
***
label var b_avg "b_avg"
label var a_avg "a_avg"
label var b_aex "b_aex"
label var a_aex "a_aex"
label var b_stock "b_stock"
label var a_stock "a_stock"
label var b_msci "b_msci"
label var a_msci "a_msci"
label var b_bitcoin "b_bitcoin"
label var a_bitcoin "a_bitcoin"

label var age "Age"
label var dfemale "Female"
label var single_dummy "Single"
label var aantalki "Number of kids"
label var oplcat "Education"
label var nettohh "Family Income"
label var occ_empl "Employed"
label var occ_retired "Retired"
label var occ_unemp "Unemployed"
label var finass_ms "Financial Wealth"
label var logfinass_md "Wealth Imputed"
label var finasshh_ms "HH Financial Wealth"
label var logfinasshh_md "HH Wealth Imputed"
label var FL_Basic "FL Basic"
label var FL_Advanced "FL Advanced"
label var FL_Total "Financial Literacy"
label var b_risk "Risk Aversion"
label var a_risk "Likelihood Insensitivity"
label var dpos_b_risk "Indicator for Risk aversion > 0"
label var dpos_a_risk "Indicator for L. Insensitivity > 0"
label var avg_riskprem12 "Alt. Risk Aversion"
label var InvS "Alt. Inverse-S"

label var logchildren "Number of kids (log)"
label var lognettohh "Family Income (log)"
label var logfinass_ms "Financial Wealth (log)"
label var logfinasshh_ms "HH Financial Wealth (log)"

label var dknow_stock "Knows a Familiar Stock"
label var dstock_past "Invested in Familiar Stock in Past"
label var dstock_now "Invests in Familiar Stock Now"
label var dknow_msci "Familiar with MSCI World"
label var dmsci_past "Invested in MSCI World in Past"
label var dmsci_now "Invests in MSCI World Now"
label var dknow_bitcoin "Familiar with Bitcoin"
label var dcrypto_past "Invested in Crypto Currency in Past"
label var dcrypto_now "Invests in Crypto Currency Now"


************************************************************************
* 
* Generate Table 1, 3, A1, and B1 in the article, using the main dataset
* These tables show summary statistics
* 
************************************************************************

* Table 1: Summary statistics of ambiguity attitudes 
*
* Panel A: Ambiguity aversion/seeking
sum b_aex b_stock b_msci b_bitcoin b_avg if $screen0
eststo clear
estpost summ b_aex b_stock b_msci b_bitcoin b_avg if $screen0, detail
esttab using AKMP_Table1.rtf, b(2) replace wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0))") noobs nonumber compress ///
   title("Table 1, Panel A: Ambiguity Aversion Descriptive Statistics")
*
* Panel B: index a
sum a_aex a_stock a_msci a_bitcoin a_avg if $screen0
eststo clear
estpost summ a_aex a_stock a_msci a_bitcoin a_avg if $screen0, detail
esttab using AKMP_Table1.rtf, b(2) append  wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0)) ") noobs nonumber compress ///   
   title("Panel B: A-Insensitivity Descriptive Statistics")    


* Table 3: Summary statistics index a and perceived ambiguity
*
* Panel A: Negative Values of Index a and Violations of Monotonicity
estpost summ db01_a_aex dlt0_a_aex dgt1_a_aex if $screen0, detail
esttab e(mean,fmt(3)) using AKMP_Table3.rtf, replace coeflabels(r1 a_aex) nomtitles collabels("% 0 <= a <= 1" "% a < 0" "% a > 1") ///
   title("Table 3, Panel A: Negative Values of Index a and Violations of Monotonicity")
estpost summ db01_a_stock dlt0_a_stock dgt1_a_stock if $screen0, detail
esttab e(mean,fmt(3)) using AKMP_Table3.rtf, append coeflabels(r1 a_stock) nomtitles collabels("% 0 <= a <= 1" "% a < 0" "% a > 1")
estpost summ db01_a_msci dlt0_a_msci dgt1_a_msci if $screen0, detail
esttab e(mean,fmt(3)) using AKMP_Table3.rtf, append coeflabels(r1 a_msci) nomtitles collabels("% 0 <= a <= 1" "% a < 0" "% a > 1")
estpost summ db01_a_bitcoin dlt0_a_bitcoin dgt1_a_bitcoin  if $screen0, detail
esttab e(mean,fmt(3)) using AKMP_Table3.rtf, append coeflabels(r1 a_bitcoin) nomtitles collabels("% 0 <= a <= 1" "% a < 0" "% a > 1")
estpost summ db01_a_avg dlt0_a_avg dgt1_a_avg   if $screen0, detail
esttab e(mean,fmt(3)) using AKMP_Table3.rtf, append coeflabels(r1 a_avg) nomtitles collabels("% 0 <= a <= 1" "% a < 0" "% a > 1")
*
* Panel B: Perceived ambiguity 
eststo clear
estpost summ ap_aex ap_stock ap_msci ap_bitcoin ap_avg if $screen0, detail
esttab using AKMP_Table3.rtf, b(2) append wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0)) ") noobs nonumber compress ///
   title("Table 3, Panel B: Summary Statistics of Perceived Ambiguity")

   
* Appendix A
*
* Table A1: Summary statistics of all control variables and dependent variables
global vars_sociodem    age dfemale single_dummy aantalki oplcat occ_empl occ_retired nettohh finasshh_ms 
global vars_risk        b_risk dpos_b_risk a_risk dpos_a_risk 
global vars_investments FL_Total dknow_stock dstock_now dcrypto_now dmsci_now
sum $vars_sociodem $vars_risk $vars_investments if $screen0
eststo clear
estpost summ $vars_sociodem      if $screen0, detail
esttab using AKMP_TableA1.rtf, b(2) replace wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2))") noobs nonumber label ///
   title("Appendix A: Table A1, Descriptive Statistics of the DHS Investor Dataset")
estpost summ $vars_risk          if $screen0, detail
esttab using AKMP_TableA1.rtf, b(2) append  wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2))") noobs nonumber label
estpost summ $vars_investments   if $screen0, detail
esttab using AKMP_TableA1.rtf, b(3) append  wide cells("mean(fmt(3)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2))") noobs nonumber label


* Appendix B
*
* Table B1: summary statistics of ambiguity attitudes for non-investors 
*
* Panel A: Ambiguity aversion/seeking
sum b_aex b_stock b_msci b_bitcoin b_avg if $screen0_noninvest
eststo clear
estpost summ b_aex b_stock b_msci b_bitcoin b_avg if $screen0_noninvest, detail
esttab using AKMP_TableB1.rtf, b(2) replace wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0))") noobs nonumber compress  ///
   title("Appendix B: Table B1, Panel A: Ambiguity Aversion Descriptive Statistics in the Non-Investor Sample")  
*
* Panel B: A-Insensitivity
sum a_aex a_stock a_msci a_bitcoin a_avg if $screen0_noninvest
eststo clear
estpost summ a_aex a_stock a_msci a_bitcoin a_avg if $screen0_noninvest, detail
esttab using AKMP_TableB1.rtf, b(2) append wide cells("mean(fmt(2)) p50(fmt(2)) sd(fmt(2)) min(fmt(2)) max(fmt(2)) count(fmt(0))") noobs nonumber compress  ///
   title("Panel B: A-Insensitivity Descriptive Statistics in the Non-Investor Sample")    

* Tests for differences in mean of index b between investors and non-investors
* Used for the significance stars in column 1 (Mean) of Table B1, Panel A
ttest b_aex     if $screen0_all, by(beleg)
ttest b_stock   if $screen0_all, by(beleg)
ttest b_msci    if $screen0_all, by(beleg)
ttest b_bitcoin if $screen0_all, by(beleg)
ttest b_avg     if $screen0_all, by(beleg)

* Tests for differences in median of index b between investors and non-investors
* Used for the significance stars in column 2 (Median) of Table B1, Panel A
ranksum b_aex     if $screen0_all, by(beleg)
ranksum b_stock   if $screen0_all, by(beleg)
ranksum b_msci    if $screen0_all, by(beleg)
ranksum b_bitcoin if $screen0_all, by(beleg)
ranksum b_avg     if $screen0_all, by(beleg)

* Tests for differences in mean of index a between investors and non-investors
* Used for the significance stars in column 1 (Mean) of Table B1, Panel B
ttest a_aex     if $screen0_all, by(beleg)
ttest a_stock   if $screen0_all, by(beleg)
ttest a_msci    if $screen0_all, by(beleg)
ttest a_bitcoin if $screen0_all, by(beleg)
ttest a_avg     if $screen0_all, by(beleg)

* Tests for differences in median of index a between investors and non-investors
* Used for the significance stars in column 2 (Median) of Table B1, Panel B
ranksum a_aex     if $screen0_all, by(beleg)
ranksum a_stock   if $screen0_all, by(beleg)
ranksum a_msci    if $screen0_all, by(beleg)
ranksum a_bitcoin if $screen0_all, by(beleg)
ranksum a_avg     if $screen0_all, by(beleg)


************************************************************************
* 
* Produce the graphs shown in Figure 3, 4, and 5 of the article, 
* using the main dataset
* 
************************************************************************

* Figure 3: Ambiguity Attitudes (Aversion/Neutral/Seeking) for Investors
*
* First create the 4 sub-figures for Figure 3
set scheme s2gcolor   /* Note: set the color scheme to black and white */
graph bar dspos_b_aex dzero_b_aex dsneg_b_aex if $screen0, saving(bar_b_aex, replace) title(" Stock Index ") percentages stack  legend(label(1 "Averse") label(2 "Neutral") label(3 "Seeking")) blabel(bar, position(center) format(%2.1f)  size(medium) color(black)) aspectratio(3.6) graphregion(color(white)) bgcolor(white) intensity(*0.70) bar(2, color(gs12*1.0)) 
graph bar dspos_b_stock dzero_b_stock dsneg_b_stock if $screen0, saving(bar_b_stock, replace) title("Familiar stock") percentages stack  legend(label(1 "Averse") label(2 "Neutral") label(3 "Seeking")) blabel(bar, position(center) format(%2.1f) size(medium) color(black)) aspectratio(3.5) graphregion(color(white)) bgcolor(white) intensity(*0.70) bar(2, color(gs12*1.0))
graph bar dspos_b_msci dzero_b_msci dsneg_b_msci if $screen0, saving(bar_b_msci, replace) title("  MSCI World  ") percentages stack  legend(label(1 "Averse") label(2 "Neutral") label(3 "Seeking")) blabel(bar, position(center) format(%2.1f) size(medium) color(black)) aspectratio(3.5) graphregion(color(white)) bgcolor(white) intensity(*0.70) bar(2, color(gs12*1.0))
graph bar dspos_b_bitcoin dzero_b_bitcoin dsneg_b_bitcoin if $screen0, saving(bar_b_bitcoin, replace) title("   Bitcoin   ") percentages stack  legend(label(1 "Averse") label(2 "Neutral") label(3 "Seeking")) blabel(bar, position(center) format(%2.1f) size(medium) color(black)) aspectratio(3.75) graphregion(color(white)) bgcolor(white) intensity(*0.70) bar(2, color(gs12*1.0))

* Create Figure 3 
* Note: the next command requires the user package "grc1leg", to combine graphs with a single legend
* It can be installed with the following command:
* net install grc1leg, from( http://www.stata.com/users/vwiggins/)
grc1leg bar_b_aex.gph bar_b_stock.gph bar_b_msci.gph bar_b_bitcoin.gph, title("Ambiguity Aversion") legendfrom(bar_b_aex.gph) rows(1) saving(AKMP_Figure3.gph, replace) graphregion(color(white)) 

* Export Figure 3 in "png" format
graph export AKMP_Figure3.png, replace
* graph export AKMP_Figure3.eps, replace

* Remove temporary files
rm "bar_b_aex.gph"
rm "bar_b_stock.gph" 
rm "bar_b_msci.gph"
rm "bar_b_bitcoin.gph" 
rm "AKMP_Figure3.gph"


* Figure 4: Scatter Plots Ambiguity Aversion
*
* First create the 6 sub-figures for Figure 4
corr b_stock b_aex if $screen0
local corr : di %4.2f r(rho)
twoway scatter b_stock b_aex     if $screen0 || lfit b_stock b_aex     if $screen0 , saving(scatb_stock_aex, replace) ytitle("b_stock") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

corr b_msci b_aex  if $screen0
local corr : di %4.2f r(rho)
twoway scatter b_msci b_aex      if $screen0 || lfit b_msci b_aex      if $screen0 , saving(scatb_msci_aex, replace) ytitle("b_msci") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

corr b_bitcoin b_aex   if $screen0
local corr : di %4.2f r(rho)
twoway scatter b_bitcoin b_aex   if $screen0 || lfit b_bitcoin b_aex   if $screen0 , saving(scatb_bitcoin_aex, replace) ytitle("b_bitcoin") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

corr b_msci b_stock   if $screen0
local corr : di %4.2f r(rho)
twoway scatter b_bitcoin b_stock if $screen0 || lfit b_msci b_stock    if $screen0 , saving(scatb_msci_stock, replace) ytitle("b_msci") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

corr b_bitcoin b_stock   if $screen0
local corr : di %4.2f r(rho)
twoway scatter b_bitcoin b_stock if $screen0 || lfit b_bitcoin b_stock if $screen0 , saving(scatb_bitcoin_stock, replace) ytitle("b_bitcoin") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

corr b_bitcoin b_msci   if $screen0
local corr : di %4.2f r(rho)
twoway scatter b_msci b_bitcoin  if $screen0 || lfit b_bitcoin b_msci  if $screen0 , saving(scatb_bitcoin_msci, replace) ytitle("b_bitcoin") xtitle("b_msci") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

* Factor analysis for percentage explain by 1st factor
pwcorr b_aex b_stock b_msci b_bitcoin if $screen0, sig obs
matrix R=r(C)
factormat R, n(295) pcf

* Create Figure 4 
graph combine scatb_stock_aex.gph scatb_msci_aex.gph scatb_bitcoin_aex.gph scatb_msci_stock.gph scatb_bitcoin_stock.gph scatb_bitcoin_msci.gph,  title("Ambiguity Aversion", linegap(1.5)) subtitle("Percentage explained by 1st factor: 76.8%", linegap(1.25)) saving(AKMP_Figure4.gph, replace) graphregion(color(white)) 

* Export Figure 4 in "png" format
graph export AKMP_Figure4.png, replace
* graph export AKMP_Figure4.eps, replace

* Remove temporary files
rm "scatb_stock_aex.gph" 
rm "scatb_msci_aex.gph" 
rm "scatb_bitcoin_aex.gph" 
rm "scatb_msci_stock.gph" 
rm "scatb_bitcoin_stock.gph" 
rm "scatb_bitcoin_msci.gph"
rm "AKMP_Figure4.gph"


* Figure 5: Scatter Plots Perceived Ambiguity 
*
* First create the 6 sub-figures for Figure 5
global screen0pw a_stock >= 0 & a_stock <= 1 & a_aex >= 0 & a_aex <= 1 & beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
corr a_stock a_aex if $screen0pw
local corr : di %4.2f r(rho)
twoway scatter a_stock a_aex     if $screen0pw  || lfit a_stock a_aex     if $screen0pw , saving(scata_stock_aex, replace) ytitle("a_stock") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

global screen0pw a_msci >= 0 & a_msci <= 1 & a_aex >= 0 & a_aex <= 1 & beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
corr a_msci a_aex  if $screen0pw & a_msci >= 0 & a_msci <= 1 & a_aex >= 0 & a_aex <= 1 
local corr : di %4.2f r(rho)
twoway scatter a_msci a_aex      if $screen0pw || lfit a_msci a_aex      if $screen0pw , saving(scata_msci_aex, replace) ytitle("a_msci") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

global screen0pw a_bitcoin >= 0 & a_bitcoin <= 1 & a_aex >= 0 & a_aex <= 1 & beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
corr a_bitcoin a_aex   if $screen0pw 
local corr : di %4.2f r(rho)
twoway scatter a_bitcoin a_aex   if $screen0pw || lfit a_bitcoin a_aex   if $screen0pw , saving(scata_bitcoin_aex, replace) ytitle("a_bitcoin") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white)  

global screen0pw a_msci >= 0 & a_msci <= 1 & a_stock >= 0 & a_stock <= 1 & beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
corr a_msci a_stock   if $screen0pw
local corr : di %4.2f r(rho)
twoway scatter a_msci a_stock if $screen0pw || lfit a_msci a_stock    if $screen0pw , saving(scata_msci_stock, replace) ytitle("a_msci") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 
* xscale(range(-1 3)) xlabel(-1 0 1 2 3) 

global screen0pw a_bitcoin >= 0 & a_bitcoin <= 1 & a_stock >= 0 & a_stock <= 1 & beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
corr a_bitcoin a_stock   if $screen0pw
local corr : di %4.2f r(rho)
twoway scatter a_bitcoin a_stock if $screen0pw || lfit a_bitcoin a_stock if $screen0pw , saving(scata_bitcoin_stock, replace) ytitle("a_bitcoin") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 
* xscale(range(-1 3)) xlabel(-1 0 1 2 3) 

global screen0pw a_bitcoin >= 0 & a_bitcoin <= 1 & a_msci >= 0 & a_msci <= 1 & beleg == 1 & b_aex < . & b_stock < . & b_msci < . & b_bitcoin < . & a_aex < . & a_stock < . & a_msci < . & a_bitcoin < . & avg_riskprem < . 
corr a_bitcoin a_msci   if $screen0pw
local corr : di %4.2f r(rho)
twoway scatter a_msci a_bitcoin  if $screen0pw || lfit a_bitcoin a_msci  if $screen0pw, saving(scata_bitcoin_msci, replace) ytitle("a_bitcoin") xtitle("a_msci") subtitle(r = `corr'{sup:***}) legend(off) graphregion(color(white)) bgcolor(white) 

* Factor analysis for percentage explain by 1st factor, perceived ambiguity
pwcorr ap_aex ap_stock ap_msci ap_bitcoin if $screen0, sig obs
matrix R=r(C)
factormat R, n(136) pcf
alpha ap_aex ap_msci ap_stock ap_bitcoin if $screen0, item asis

* Create Figure 5
graph combine scata_stock_aex.gph scata_msci_aex.gph scata_bitcoin_aex.gph scata_msci_stock.gph scata_bitcoin_stock.gph scata_bitcoin_msci.gph,  title("Perceived Ambiguity", linegap(1.5)) subtitle("Percentage explained by 1st factor: 59.7%", linegap(1.25))  saving(AKMP_Figure5.gph, replace) graphregion(color(white)) 

* Export Figure 4 in "png" format
graph export AKMP_Figure5.png, replace
* graph export AKMP_Figure5.eps, replace

* Remove temporary files
rm "scata_stock_aex.gph" 
rm "scata_msci_aex.gph" 
rm "scata_bitcoin_aex.gph"
rm "scata_msci_stock.gph"
rm "scata_bitcoin_stock.gph"
rm "scata_bitcoin_msci.gph"
rm "AKMP_Figure5.gph"



************************************************************************
* Save the main dataset, to keep the new variable labels
************************************************************************
save $fn_main, replace



************************************************************************
* 
* Generate the regression results in Table 2, 4, and 5 of the article, 
* using the panel dataset
* 
************************************************************************

* Open the latest panel dataset
use $fn_panel, clear

*** Label all the variables for the tables
***
label variable a "Index a"
label variable b "Index b"
label variable d_aex "Dummy aex"
label variable d_stock "Dummy stock"
label variable d_msci "Dummy msci"
label variable d_bitcoin "Dummy bitcoin"

label var age "Age"
label var dfemale "Female"
label var single_dummy "Single"
label var aantalki "Number of kids"
label var oplcat "Education"
label var nettohh "Family Income"
label var occ_empl "Employed"
label var occ_retired "Retired"
label var occ_unemp "Unemployed"
label var finasshh_ms "HH Financial Wealth"
label var logfinasshh_md "HH Wealth Imputed"
label var logfinasshh_ms "HH Financial Wealth (log)"
label var FL_Basic "FL Basic"
label var FL_Advanced "FL Advanced"
label var FL_Total "Financial Literacy"
label var b_risk "Risk Aversion"
label var a_risk "Likelihood Insensitivity"
label var logchildren "Number of kids (log)"
label var lognettohh "Family Income (log)"


*** Estimate the panel model regressions for Table2, index b (Ambiguity Aversion)
***
eststo clear

* Model 1 for index b: constant and random effects only
mixed b || invid:  if $screen0p, cluster(invid) 
estimates store bmodr1
predict b1_xb  if $screen0p, xb
predict b1_hat if $screen0p, fitted 
est restore bmodr1
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rconst = exp(_b[lns1_1_1:_cons])
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant)
estadd scalar var_rconst = (sd_rconst)^2
* Var(random slope bitcoin)
estadd scalar var_rsbit  =  0
* Var(observed)
summ b1_xb if $screen0p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)


* Model 2 for index b: add dummies for the 4 sources (test source dependence)
mixed b d_stock d_msci d_bitcoin || invid:   if $screen0p, cluster(invid) 
estimates store bmodr2
predict b2_xb  if $screen0p, xb
predict b2_hat if $screen0p, fitted 
est restore bmodr2
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rconst = exp(_b[lns1_1_1:_cons])
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant)
estadd scalar var_rconst = (sd_rconst)^2
* Var(random slope bitcoin)
estadd scalar var_rsbit  =  0
* Var(observed)
summ b2_xb if $screen0p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)


* Model 3 for index b: adds random slope for Bitcoin
eststo clear
mixed b d_stock d_msci d_bitcoin || invid:d_bitcoin if $screen0p, cluster(invid) cov(uns) stddev
estimates store bmodr3
predict b3_xb if $screen0p, xb
predict b3_hat if $screen0p, fitted 
est restore bmodr3
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rsbit  = exp(_b[lns1_1_1:_cons])
scalar sd_rconst = exp(_b[lns1_1_2:_cons])
scalar correl1   = tanh(_b[atr1_1_1_2:_cons])
scalar covar1    = correl1*sd_rsbit*sd_rconst
* Note: random slope bitcoin applies to only 25% of the observations, when d_bitcoin = 1, which is captured by "*mean_dbitcoin" (= x 0.25)
summ d_bitcoin if $screen0p, detail
scalar mean_dbitcoin = r(mean)
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant). 	 Note: 50% of the covariance between the 2 random effects also attributed
estadd scalar var_rconst = (sd_rconst)^2 + mean_dbitcoin*covar1
* Var(random slope bitcoin). Note: 50% of the covariance between the 2 random effects also attributed
estadd scalar var_rsbit  =  mean_dbitcoin*(sd_rsbit)^2 + mean_dbitcoin*covar1 
* Var(observed)
summ b3_xb if $screen0p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_rsbit) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)


* Model 4 for index b: add socio-demographic vars (observable)
* Adds random slope for Bitcoin
mixed b d_stock d_msci d_bitcoin $controls_demo || invid:d_bitcoin if $screen0p, cluster(invid) cov(uns) stddev
estimates store bmodr4
predict b4_xb if $screen0p, xb
predict b4_hat if $screen0p, fitted 
est restore bmodr4
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rsbit  = exp(_b[lns1_1_1:_cons])
scalar sd_rconst = exp(_b[lns1_1_2:_cons])
scalar correl1   = tanh(_b[atr1_1_1_2:_cons])
scalar covar1    = correl1*sd_rsbit*sd_rconst
* Note: random slope bitcoin applies to only 25% of the observations, when d_bitcoin = 1, which is captured by "*mean_dbitcoin" (= x 0.25)
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant)		Note: 50% of the covariance between the 2 random effects also attributed
estadd scalar var_rconst = (sd_rconst)^2 + mean_dbitcoin*covar1 
* Var(random slope bitcoin) Note: 50% of the covariance between the 2 random effects also attributed
estadd scalar var_rsbit  =  mean_dbitcoin*(sd_rsbit)^2 + mean_dbitcoin*covar1 
* Var(observed)
summ b4_xb if $screen0p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_rsbit) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)


* Model 5 for index b: adds financial literacy and risk attitudes
* Adds random slope for Bitcoin
mixed b d_stock d_msci d_bitcoin $controls_demo $controls_FLrisk || invid:d_bitcoin if $screen0p, cluster(invid) cov(uns) stddev
estimates store bmodr5
predict b5_xb if $screen0p, xb
predict b5_hat if $screen0p, fitted 
est restore bmodr5
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rsbit  = exp(_b[lns1_1_1:_cons])
scalar sd_rconst = exp(_b[lns1_1_2:_cons])
scalar correl1   = tanh(_b[atr1_1_1_2:_cons])
scalar covar1    = correl1*sd_rsbit*sd_rconst
* Note: Random slope bitcoin applies to only 25% of the observations, when d_bitcoin = 1, which is Captured by "*mean_dbitcoin" (= x 0.25)
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant)		Note: 50% of the covariance between the 2 random effects also attributed
estadd scalar var_rconst = (sd_rconst)^2 + mean_dbitcoin*covar1 
* Var(random slope bitcoin) Note: 50% of the covariance between the 2 random effects also attributed
estadd scalar var_rsbit  =  mean_dbitcoin*(sd_rsbit)^2 + mean_dbitcoin*covar1 
* Var(observed)
summ b5_xb if $screen0p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_rsbit) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)
* Save the estimates to a file
estimates save "bmodr5.ster", replace

* Table 2: Analysis of Heterogeneity in Ambiguity Aversion
*
* Export the results to a Word document
* 
esttab bmodr1 bmodr2 bmodr3 bmodr4 bmodr5 using "AKMP_Table2.rtf", replace rtf order(_cons d_stock d_msci d_bitcoin) b(3) se(3) pr2 not nogaps /// 
   starlevels(* 0.1 ** 0.05 *** 0.01) label compress drop(lns*: atr*:)  ///
   stats(N icc2 var_err var_rconst var_rsbit var_obs pcvar_err pcvar_rconst pcvar_rsbit pcvar_obs, fmt(0 2 3 3 3 3 0 0 0 0)) ///
   title("Table 2: Analysis of Heterogeneity in Ambiguity Aversion")


   
*** Estimate the panel model regressions for Table 4, Perceived Ambiguity 
*** Dependent variable: index a, limited to 0 <= a <= 1 with $screen2p
***

* Model 1 for index a: constant and random effects only
mixed a || invid:  if $screen2p, cluster(invid) 
estimates store amodr1
predict a1_xb  if $screen2p, xb
predict a1_hat if $screen2p, fitted 
est restore amodr1
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rconst = exp(_b[lns1_1_1:_cons])
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant)
estadd scalar var_rconst = (sd_rconst)^2
* Var(random slope bitcoin)
estadd scalar var_rsbit  =  0
* Var(random slope stock)
estadd scalar var_rsstock =  0
* Var(observed)
summ a1_xb if $screen2p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_rsstock= 100*e(var_rsstock)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)

* Model 2 for index a: add dummies for the 4 sources (test source dependence)
mixed a d_stock d_msci d_bitcoin || invid:   if $screen2p, cluster(invid) stddev 
estimates store amodr2
predict a2_xb  if $screen2p, xb
predict a2_hat if $screen2p, fitted 
est restore amodr2
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rconst = exp(_b[lns1_1_1:_cons])
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant)
estadd scalar var_rconst = (sd_rconst)^2
* Var(random slope bitcoin)
estadd scalar var_rsbit  =  0
* Var(random slope stock)
estadd scalar var_rsstock =  0
* Var(observed)
summ a2_xb if $screen2p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_rsstock= 100*e(var_rsstock)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)
  
* Model 3 for index a: add dummies for the 4 sources (test source dependence)
* Adds random slope for bitcoin and stock 
eststo clear
mixed a d_stock d_msci d_bitcoin || invid:d_bitcoin d_stock if $screen2p, cluster(invid) cov(uns) stddev 
estimates store amodr3
predict a3_xb if $screen2p, xb
predict a3_hat if $screen2p, fitted 
est restore amodr3
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rsbit  = exp(_b[lns1_1_1:_cons])
scalar sd_rsstock= exp(_b[lns1_1_2:_cons])
scalar sd_rconst = exp(_b[lns1_1_3:_cons])
scalar correl1   = tanh(_b[atr1_1_1_2:_cons])
scalar correl2   = tanh(_b[atr1_1_1_3:_cons])
scalar correl3   = tanh(_b[atr1_1_2_3:_cons])
scalar covar1    = correl1*sd_rsbit*sd_rsstock
scalar covar2    = correl2*sd_rsbit*sd_rconst
scalar covar3    = correl3*sd_rsstock*sd_rconst
* Note: random slope bitcoin applies to only approx. 25% of the observations, when d_bitcoin = 1, which is captured by "*mean_dbitcoin" 
summ d_bitcoin if $screen2p, detail
scalar mean_dbitcoin = r(mean)
* Note: random slope stock applies to only approx. 25% of the observations, when d_stock = 1, which is captured by "*mean_dstock" 
summ d_stock if $screen2p, detail
scalar mean_dstock = r(mean)
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant). 	 Note: 50% of the covariance terms also attributed
estadd scalar var_rconst  = (sd_rconst)^2 + mean_dbitcoin*covar2 + mean_dstock*covar3
* Var(random slope bitcoin). Note: 50% of the covariance terms also attributed
* Note: covar1 terms drops out because always one of dummies d_stock and d_bitcoin is equal to zero
estadd scalar var_rsbit   =  mean_dbitcoin*(sd_rsbit)^2 + mean_dbitcoin*covar2  
* Var(random slope stock).  Note: 50% of the covariance terms also attributed
estadd scalar var_rsstock =  mean_dstock*(sd_rsstock)^2 + mean_dstock*covar3
* Var(observed)
summ a3_xb if $screen2p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_rsbit) + e(var_rsstock) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_rsstock= 100*e(var_rsstock)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)

* Model 4 for index a: add socio-demographic vars (observable)
* Adds random slope for bitcoin and stock 
mixed a d_stock d_msci d_bitcoin $controls_demo || invid:d_bitcoin d_stock if $screen2p, cluster(invid) cov(uns) stddev 
estimates store amodr4
predict a4_xb if $screen2p, xb
predict a4_hat if $screen2p, fitted 
est restore amodr4
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rsbit  = exp(_b[lns1_1_1:_cons])
scalar sd_rsstock= exp(_b[lns1_1_2:_cons])
scalar sd_rconst = exp(_b[lns1_1_3:_cons])
scalar correl1   = tanh(_b[atr1_1_1_2:_cons])
scalar correl2   = tanh(_b[atr1_1_1_3:_cons])
scalar correl3   = tanh(_b[atr1_1_2_3:_cons])
scalar covar1    = correl1*sd_rsbit*sd_rsstock
scalar covar2    = correl2*sd_rsbit*sd_rconst
scalar covar3    = correl3*sd_rsstock*sd_rconst
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant). 	 Note: 50% of the covariance terms also attributed
estadd scalar var_rconst  = (sd_rconst)^2 + mean_dbitcoin*covar2 + mean_dstock*covar3
* Var(random slope bitcoin). Note: 50% of the covariance terms also attributed
* Note: covar1 terms drops out because always one of dummies d_stock and d_bitcoin is equal to zero
estadd scalar var_rsbit   =  mean_dbitcoin*(sd_rsbit)^2 + mean_dbitcoin*covar2  
* Var(random slope stock).  Note: 50% of the covariance terms also attributed
estadd scalar var_rsstock =  mean_dstock*(sd_rsstock)^2 + mean_dstock*covar3
* Var(observed)
summ a4_xb if $screen2p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_rsbit) + e(var_rsstock) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_rsstock= 100*e(var_rsstock)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)

* Model 5 for index a: add other vars, Financial Literacy and Risk attitudes
* Adds random slope for bitcoin and stock 
mixed a d_stock d_msci d_bitcoin $controls_demo $controls_FLrisk || invid:d_bitcoin d_stock if $screen2p, cluster(invid) cov(uns) stddev 
estimates store amodr5
predict a5_xb if $screen2p, xb
predict a5_hat if $screen2p, fitted 
est restore amodr5
* ICC
estat icc
estadd r(icc2)
* Input for variance decomposition
scalar sd_err    = exp(_b[lnsig_e:_cons])
scalar sd_rsbit  = exp(_b[lns1_1_1:_cons])
scalar sd_rsstock= exp(_b[lns1_1_2:_cons])
scalar sd_rconst = exp(_b[lns1_1_3:_cons])
scalar correl1   = tanh(_b[atr1_1_1_2:_cons])
scalar correl2   = tanh(_b[atr1_1_1_3:_cons])
scalar correl3   = tanh(_b[atr1_1_2_3:_cons])
scalar covar1    = correl1*sd_rsbit*sd_rsstock
scalar covar2    = correl2*sd_rsbit*sd_rconst
scalar covar3    = correl3*sd_rsstock*sd_rconst
* Variance decomposition: 
* Var(error)
estadd scalar var_err = (sd_err)^2
* Var(random constant). 	 Note: 50% of the covariance terms also attributed
estadd scalar var_rconst  = (sd_rconst)^2 + mean_dbitcoin*covar2 + mean_dstock*covar3
* Var(random slope bitcoin). Note: 50% of the covariance terms also attributed
* Note: covar1 terms drops out because always one of dummies d_stock and d_bitcoin is equal to zero
estadd scalar var_rsbit   =  mean_dbitcoin*(sd_rsbit)^2 + mean_dbitcoin*covar2  
* Var(random slope stock).  Note: 50% of the covariance terms also attributed
estadd scalar var_rsstock =  mean_dstock*(sd_rsstock)^2 + mean_dstock*covar3
* Var(observed)
summ a5_xb if $screen2p, detail
estadd scalar var_obs = r(Var)
* Var(total)
estadd scalar var_total = e(var_err) + e(var_rconst) + e(var_rsbit) + e(var_rsstock) + e(var_obs)
* Calculate % contribution to total variance
estadd scalar pcvar_err    = 100*e(var_err)/e(var_total)
estadd scalar pcvar_rconst = 100*e(var_rconst)/e(var_total)
estadd scalar pcvar_rsbit  = 100*e(var_rsbit)/e(var_total)
estadd scalar pcvar_rsstock= 100*e(var_rsstock)/e(var_total)
estadd scalar pcvar_obs    = 100*e(var_obs)/e(var_total)
* Save the estimates to a file
estimates save "amodr5.ster", replace

* Table 4: Analysis of Heterogeneity in Perceived Ambiguity
*
* Export the results to a Word document
* 
esttab amodr1 amodr2 amodr3 amodr4 amodr5 using "AKMP_Table4.rtf", replace rtf order(_cons d_stock d_msci d_bitcoin) b(3) se(3) pr2 not nogaps ///
	stats(N icc2 var_err var_rconst var_rsbit var_rsstock var_obs pcvar_err pcvar_rconst pcvar_rsbit pcvar_rsstock pcvar_obs, fmt(0 2 3 3 3 3 3 0 0 0 0 0)) ///
	starlevels(* 0.1 ** 0.05 *** 0.01)  label compress drop(lns*: atr*:) ///
	title("Table 4: Analysis of Heterogeneity in Perceived Ambiguity")

     

*** Estimate the panel probit regressions for Table 5, Investment
***

* Get the means of a and b for the marginal effects
summ dinv a if $screen0p & dinv < .
local a_mean = r(mean) 
summ dinv b if $screen0p & dinv < .
local b_mean = r(mean) 

* Table 5, Columns 1-3: index a and b 
*
* Model 1: without controls
eststo clear
probit dinv a b d_msci d_bitcoin  if $screen0p, vce(cluster invid)
scalar r2p = e(r2_p)
margins, dydx(*) at(a=(`a_mean') b=(`b_mean')) post
est store tab5mod1
estadd scalar r2_p = r2p

* Model 2: with socio-demographic controls
probit dinv a b d_msci d_bitcoin $controls_demo if $screen0p, vce(cluster invid) 
scalar r2p = e(r2_p)
margins, dydx(*) at(a=(`a_mean') b=(`b_mean')) post
est store tab5mod2
estadd scalar r2_p = r2p

* Model 3: with FL, risk attitude and socio-demographic controls
probit dinv a b d_msci d_bitcoin $controls_demo $controls_FLrisk  if $screen0p, vce(cluster invid)
scalar r2p = e(r2_p)
margins, dydx(*) at(a=(`a_mean') b=(`b_mean')) post
est store tab5mod3
estadd scalar r2_p = r2p

* Table 5, Columns 4-6: fitted index a and b from Model 3 in Table 4, 
*                       with restriction 0 <= a < = 1 (perceived ambiguity)
*
label variable a3_hat "Perc. ambiguity (fitted)"
label variable b3_hat "Amb. aversion (fitted)"

* Get the means of a3_hat and b3_hat for the marginal effects
summ dinv a3_hat if $screen2p & dinv < .
local a3h_mean = r(mean) 
summ dinv b3_hat if $screen2p & dinv < .
local b3h_mean = r(mean) 

* Model 4: without controls
probit dinv a3_hat b3_hat d_msci d_bitcoin if $screen0p, vce(cluster invid)
scalar r2p = e(r2_p)
margins, dydx(*) at(a3_hat=(`a3h_mean') b3_hat=(`b3h_mean')) post
est store tab5mod4
estadd scalar r2_p = r2p

* Model 5: with socio-demographic controls
probit dinv a3_hat b3_hat d_msci d_bitcoin $controls_demo if $screen0p, vce(cluster invid) 
scalar r2p = e(r2_p)
margins, dydx(*) at(a3_hat=(`a3h_mean') b3_hat=(`b3h_mean')) post
est store tab5mod5
estadd scalar r2_p = r2p

* Model 6: with FL, risk attitude and socio-demographic controls
probit dinv a3_hat b3_hat d_msci d_bitcoin $controls_demo $controls_FLrisk if $screen0p, vce(cluster invid)
scalar r2p = e(r2_p)
margins, dydx(*) at(a3_hat=(`a3h_mean') b3_hat=(`b3h_mean')) post
est store tab5mod6
estadd scalar r2_p = r2p

* Table 5: Investment in the Familiar Stock, MSCI World and Crypto-Currencies
*
* Export the results to a Word document
* 
esttab tab5mod1 tab5mod2 tab5mod3 tab5mod4 tab5mod5 tab5mod6 using "AKMP_Table5.rtf", replace rtf /// 
   order(a b a3_hat b3_hat) b(3) se(3) pr2 not nogaps starlevels(* 0.1 ** 0.05 *** 0.01) ///
   stats(N r2_p, fmt(0 3)) label compress ///
   title("Table 5: Investment in the Familiar Stock, MSCI World and Crypto-Currencies")

   
   
************************************************************************
* Save the panel dataset, to keep the new variables a3_hat and b3_hat 
************************************************************************
   
save $fn_panel, replace
   
   